// File Name: led_ctrl.c
// Author: kukukukuku~
// Date: 2024-06-25

#include "./User/LED/inc/led_ctrl.h"

//LED对象
LED led1 = 
{
    .gpioPort        = LED1_GPIO_PORT,
    .gpioPin         = LED1_GPIO_PIN,
    .tmr             = LED1_TMR,
    .tmrPulse        = LED1_TMR_PULSE,
    .tmrCurrentPulse = LED1_TMR_PULSE,
    .breathStep      = 10,
    .blinkNum        = 500,
    .blinkCnt        = 0,
    .gpioPinSource   = LED1_GPIO_MUX_SOURCE,
    .gpioMux         = LED1_GPIO_MUX,
    .tmrCh           = LED1_TMR_CH,
    .mode            = LED_MODE_BREATH,
    .breathDir       = 0,
    .state           = LED_STATE_OFF,
}, 
led2 = 
{
    .gpioPort        = LED2_GPIO_PORT,
    .gpioPin         = LED2_GPIO_PIN,
    .tmr             = LED2_TMR,
    .tmrPulse        = LED2_TMR_PULSE,
    .tmrCurrentPulse = LED2_TMR_PULSE,
    .breathStep      = 10,
    .blinkNum        = 500,
    .blinkCnt        = 0,
    .gpioPinSource   = LED2_GPIO_MUX_SOURCE,
    .gpioMux         = LED2_GPIO_MUX,
    .tmrCh           = LED2_TMR_CH,
    .mode            = LED_MODE_BLINK,
    .breathDir       = 0,
    .state           = LED_STATE_OFF,
};

uint8_t led1Switch; //LED1开关
uint8_t led2Switch; //LED2开关

/**
 * @brief   LED初始化
 * @param   None
 * @retval  None
 * @note    静态函数
*/
static void led_init(void)
{
    gpio_init_type gpioInitStruct = 
    {
        .gpio_pins           = LED1_GPIO_PIN,
        .gpio_out_type       = GPIO_OUTPUT_PUSH_PULL,
        .gpio_pull           = GPIO_PULL_NONE,
        .gpio_mode           = GPIO_MODE_MUX,
        .gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER,
    };
    
    tmr_output_config_type tmrOutputConfigStruct = 
    {
        .oc_mode         = TMR_OUTPUT_CONTROL_PWM_MODE_A,
        .oc_idle_state   = FALSE,
        .oc_polarity     = TMR_OUTPUT_ACTIVE_HIGH,
        .oc_output_state = TRUE,
    };
    
    //LED GPIO初始化
    crm_periph_clock_enable(LED1_GPIO_CRM, TRUE);
    crm_periph_clock_enable(LED2_GPIO_CRM, TRUE);
    
    gpio_init(LED1_GPIO_PORT, &gpioInitStruct);
    gpioInitStruct.gpio_pins = LED2_GPIO_PIN;
    gpio_init(LED2_GPIO_PORT, &gpioInitStruct);
    gpio_pin_mux_config(LED1_GPIO_PORT, LED1_GPIO_MUX_SOURCE, LED1_GPIO_MUX);
    gpio_pin_mux_config(LED2_GPIO_PORT, LED2_GPIO_MUX_SOURCE, LED2_GPIO_MUX);
    
    //LED TMR初始化
    crm_periph_clock_enable(LED1_TMR_CRM, TRUE);
    crm_periph_clock_enable(LED2_TMR_CRM, TRUE);
    
    tmr_base_init(LED1_TMR, LED1_TMR_PR_BREATH, LED1_TMR_DIV);
    tmr_cnt_dir_set(LED1_TMR, TMR_COUNT_UP);
    tmr_clock_source_div_set(LED1_TMR, TMR_CLOCK_DIV1);
    tmr_period_buffer_enable(LED1_TMR, TRUE);
    tmr_output_channel_config(LED1_TMR, LED1_TMR_CH, &tmrOutputConfigStruct);
    tmr_channel_value_set(LED1_TMR, LED1_TMR_CH, LED1_TMR_PULSE);
    tmr_output_channel_buffer_enable(LED1_TMR, LED1_TMR_CH, FALSE);
    tmr_output_enable(LED1_TMR, TRUE);
    tmr_flag_clear(LED1_TMR, TMR_OVF_FLAG);
    tmr_interrupt_enable(LED1_TMR, TMR_OVF_INT, TRUE);
    tmr_counter_enable(LED1_TMR, TRUE);
    
    tmr_base_init(LED2_TMR, LED2_TMR_PR_BREATH, LED2_TMR_DIV);
    tmr_cnt_dir_set(LED2_TMR, TMR_COUNT_UP);
    tmr_clock_source_div_set(LED2_TMR, TMR_CLOCK_DIV1);
    tmr_period_buffer_enable(LED2_TMR, TRUE);
    tmr_output_channel_config(LED2_TMR, LED2_TMR_CH, &tmrOutputConfigStruct);
    tmr_channel_value_set(LED2_TMR, LED2_TMR_CH, LED2_TMR_PULSE);
    tmr_output_channel_buffer_enable(LED2_TMR, LED2_TMR_CH, FALSE);
    tmr_output_enable(LED2_TMR, TRUE);
    tmr_flag_clear(LED2_TMR, TMR_OVF_FLAG);
    tmr_interrupt_enable(LED2_TMR, TMR_OVF_INT, TRUE);
    tmr_counter_enable(LED2_TMR, TRUE);
    
    //LED NVIC初始化
    nvic_irq_enable(LED1_TMR_IRQ, 0, 0);
    nvic_irq_enable(LED2_TMR_IRQ, 0, 0);
}


/**
 * @brief   打开LED
 * @param   led:    LED对象
 * @retval  None
 * @note    None
*/
void led_on(pLED led)
{
    if(led->mode == LED_MODE_BREATH)
    {
        return;
    }
    
    LED_ENA ?\
    gpio_bits_set(led->gpioPort, led->gpioPin) :\
    gpio_bits_reset(led->gpioPort, led->gpioPin);
}


/**
 * @brief   关闭LED
 * @param   led:    LED对象
 * @retval  None
 * @note    None
*/
void led_off(pLED led)
{
    if(led->mode == LED_MODE_BREATH)
    {
        return;
    }
    
    LED_DISA ?\
    gpio_bits_set(led->gpioPort, led->gpioPin) :\
    gpio_bits_reset(led->gpioPort, led->gpioPin);
}


/**
 * @brief   LED配置
 * @param   None
 * @retval  None
 * @note    None
*/
void led_config(void)
{
    led_init();
}
